/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.prolificinteractive.materialcalendarview.ext;

import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class Logger2 {
    private static final HiLogLabel LABEL = new HiLogLabel(HiLog.DEBUG, 110, "seagazer");

    /**
     * From: width=%d, height=%f, text=%s
     * To  : width=%{public}d, height=%{public}f, text=%{public}s
     * 不支持类似 %02d 这样的补位
     *
     * @param logMessageFormat logMessageFormat
     * @return return
     */
    private static String replaceFormat(String logMessageFormat) {
        return logMessageFormat.replaceAll("%([d|f|s])", "%{public}$1");
    }

    public static void out(String message) {
        System.out.println("seagazer: " + message);
    }

    public static void i(String message, Object... args) {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        if (stackTrace.length > 4) {
            StackTraceElement element = stackTrace[3];
            String className = element.getClassName();
            String methodName = element.getMethodName();
            String lineNumber = "[Line:" + element.getLineNumber() + "]: ";
            message = className + " # " + methodName + lineNumber + message;
            HiLog.info(LABEL, replaceFormat(message), args);
        }
    }

    public static void d(String message, Object... args) {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        if (stackTrace.length > 4) {
            StackTraceElement element = stackTrace[3];
            String className = element.getClassName();
            String methodName = element.getMethodName();
            String lineNumber = "[Line:" + element.getLineNumber() + "]: ";
            message = className + " # " + methodName + lineNumber + message;
            HiLog.debug(LABEL, replaceFormat(message), args);
        }
    }

    public static void w(String message, Object... args) {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        if (stackTrace.length > 4) {
            StackTraceElement element = stackTrace[3];
            String className = element.getClassName();
            String methodName = element.getMethodName();
            String lineNumber = "[Line:" + element.getLineNumber() + "]: ";
            message = className + " # " + methodName + lineNumber + message;
            HiLog.warn(LABEL, replaceFormat(message), args);
        }
    }

    public static void e(String message, Object... args) {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        if (stackTrace.length > 4) {
            StackTraceElement element = stackTrace[3];
            String className = element.getClassName();
            String methodName = element.getMethodName();
            String lineNumber = "[Line:" + element.getLineNumber() + "]: ";
            message = className + " # " + methodName + lineNumber + message;
            HiLog.error(LABEL, replaceFormat(message), args);
        }
    }

    public static void f(String message, Object... args) {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        if (stackTrace.length > 4) {
            StackTraceElement element = stackTrace[3];
            String className = element.getClassName();
            String methodName = element.getMethodName();
            String lineNumber = "[Line:" + element.getLineNumber() + "]: ";
            message = className + " # " + methodName + lineNumber + message;
            HiLog.fatal(LABEL, replaceFormat(message), args);
        }
    }
}
